package sdp.group14.appdata;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import sdp.group14.communication.ServerConnection;

public class AppDataReader {

	private ServerConnection serverConnection;

	public AppDataReader()
	{
		serverConnection = new ServerConnection();
	}
	
	/**
	 * 
	 * @param action
	 * @param event
	 * @return
	 */
	public String addNewEvent(int action, Event event)
	{		
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(8);         
		nameValuePairs.add(new BasicNameValuePair("entryID", ""+ event.getEntryID()));  
        nameValuePairs.add(new BasicNameValuePair("action", ""+ action));
        nameValuePairs.add(new BasicNameValuePair("EventStatusID", ""+event.getStatusID()));  
        nameValuePairs.add(new BasicNameValuePair("CategoryID", ""+event.getCategoryID()));  
        nameValuePairs.add(new BasicNameValuePair("Description", ""+event.getDescription()));  
        nameValuePairs.add(new BasicNameValuePair("Cause", ""+event.getCause()));  
        nameValuePairs.add(new BasicNameValuePair("AllowStaffEnter", ""+event.getAllowStaffEnter()));  
        nameValuePairs.add(new BasicNameValuePair("Comments", ""+event.getAllowStaffEnter()));        
        
		String result = serverConnection.sendRequest(""+event.getEntryID(), action ,nameValuePairs);
		Log.i("AppDataReader", result);
		return result;
	}
	
	
	/**
	 * 
	 * @param user
	 * @param action
	 * @return
	 */
	public ArrayList<Room> getAllRooms(String user, int action)
	{		
		try
		{
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);         
	        nameValuePairs.add(new BasicNameValuePair("entryID", ""+ user));  
	        nameValuePairs.add(new BasicNameValuePair("action", ""+ action));
			String result = serverConnection.sendRequest(user, action,nameValuePairs);
			ArrayList<Room> myList = new ArrayList<Room>(); 
			if(result!= null)
			{
				result = result.substring(result.indexOf("{"));
				JSONObject json = new JSONObject(result);   
				JSONArray jArray = json.getJSONArray("posts");   

				for (int i = 0; i < jArray.length(); i++) {   
					JSONObject e = jArray.getJSONObject(i);   

					String s = e.getString("post");   
					JSONObject jObject = new JSONObject(s);   

					//	private int eventID, entryID, eventStatusID, categoryID, allowStaffEnter;
					//private String description, cause, comments, eventDate, dayModified, dayCreated;
					int roomID = Integer.parseInt(jObject.getString("roomID"));
					String roomNo = jObject.getString("roomNo");
					Room room = new Room(roomID, roomNo);
					myList.add(room);   
				}
			}

			return myList;
		}

		catch(Exception e)
		{
			Log.e("AppDataReader", e.getMessage());
			return null;
		}        
	}
	
	/**
	 * 
	 * @param user
	 * @param action
	 * @return
	 */
	public ArrayList<Category> getAllCategories(String user, int action)
	{		
		try
		{
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);         
	        nameValuePairs.add(new BasicNameValuePair("entryID", ""+ user));  
	        nameValuePairs.add(new BasicNameValuePair("action", ""+ action));
			String result = serverConnection.sendRequest(user, action,nameValuePairs);
			Log.i("serverConnection", result);
			ArrayList<Category> myList = new ArrayList<Category>(); 
			if(result!= null)
			{
				result = result.substring(result.indexOf("{"));
				JSONObject json = new JSONObject(result);   
				JSONArray jArray = json.getJSONArray("posts");   

				for (int i = 0; i < jArray.length(); i++) {   
					JSONObject e = jArray.getJSONObject(i);   

					String s = e.getString("post");   
					JSONObject jObject = new JSONObject(s);   

					//	private int eventID, entryID, eventStatusID, categoryID, allowStaffEnter;
					//private String description, cause, comments, eventDate, dayModified, dayCreated;

					int categoryID = Integer.parseInt(jObject.getString("categoryID"));
					String category = jObject.getString("category");
					Category c = new Category(categoryID, category);
					myList.add(c);   
				}
			}

			return myList;
		}

		catch(Exception e)
		{
			Log.e("AppDataReader", e.getMessage());
			return null;
		}        
	}
	
	/**
	 * 
	 * @param user
	 * @param action
	 * @return
	 */

	public ArrayList<Event> getAllMaintainenceRequest(String user, int action)
	{		
		try
		{
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);         
	        nameValuePairs.add(new BasicNameValuePair("entryID", ""+ user));  
	        nameValuePairs.add(new BasicNameValuePair("action", ""+ action));  
			String result = serverConnection.sendRequest(user, action, nameValuePairs);
			ArrayList<Event> myList = new ArrayList<Event>(); 
			if(result!= null)
			{
				result = result.substring(result.indexOf("{"));
				JSONObject json = new JSONObject(result);   
				JSONArray jArray = json.getJSONArray("posts");   

				for (int i = 0; i < jArray.length(); i++) {   
					JSONObject e = jArray.getJSONObject(i);   

					String s = e.getString("post");   
					JSONObject jObject = new JSONObject(s);   

					//	private int eventID, entryID, eventStatusID, categoryID, allowStaffEnter;
					//private String description, cause, comments, eventDate, dayModified, dayCreated;

					int eventID = Integer.parseInt(jObject.getString("EventID"));
					int entryID = Integer.parseInt(jObject.getString("EntryID"));
					String status = jObject.getString("EventStatusID");
					String category = jObject.getString("CategoryID");
					int allowStaffEnter = Integer.parseInt(jObject.getString("AllowStaffEnter"));
					String description = jObject.getString("Description");
					String cause = jObject.getString("Cause");
					String comments = jObject.getString("Comments");
					String eventDate = jObject.getString("EventDate");
					String dayModified = jObject.getString("DayModified");
					String dayCreated = jObject.getString("DayCreated");

					Event event = new Event(entryID, allowStaffEnter, status,
							category, description, cause, comments,
							eventDate, dayModified, dayCreated);
					event.setEventID(eventID);
					myList.add(event);   
				}
			}

			return myList;
		}

		catch(Exception e)
		{
			Log.e("AppDataReader", e.getMessage());
			return null;
		}        
	}

}
